<?php
/**
* @file $Id: PrintClassLists.php 532 2007-05-22 04:26:46Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if(!$_REQUEST['search_modfunc'] || $_REQUEST['search_modfunc']=='search' || $_FOCUS['modules_search'])
{
	DrawHeader(ProgramTitle());
	$extra['action'] .= "&_FOCUS_PDF=true";

	$extra['second_col'] .= '<CENTER><TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Period').'</TD><TD>';
	$periods_RET = DBGet(DBQuery("SELECT PERIOD_ID,TITLE FROM SCHOOL_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"));
	$extra['second_col'] .= '<SELECT name=period_id><OPTION value="">'._('All Periods').'</OPTION>';
	foreach($periods_RET as $period)
		$extra['second_col'] .= '<OPTION value="'.$period['PERIOD_ID'].'">'.$period['TITLE'].'</OPTION>';
	$extra['second_col'] .= '</SELECT></TD></TR><TR><TD align='.ALIGN_RIGHT.'>'._('Teacher').'</TD><TD>';
	$teachers_RET = DBGet(DBQuery("SELECT STAFF_ID,FIRST_NAME||' '||LAST_NAME AS TITLE FROM USERS WHERE SYEAR='".UserSyear()."' AND SCHOOLS LIKE '%".UserSchool()."%' AND PROFILE='teacher'"));
	$extra['second_col'] .= '<SELECT name=teacher_id><OPTION value="">'._('All Teachers').'</OPTION>';
	foreach($teachers_RET as $teacher)
		$extra['second_col'] .= '<OPTION value="'.$teacher['STAFF_ID'].'">'.$teacher['TITLE'].'</OPTION>';
	$extra['second_col'] .= '</SELECT></TD></TR>';
	$extra['second_col'] .= '<TR><TD align='.ALIGN_RIGHT.'>'._('Marking Period').' </TD><TD><SELECT name=mp_id>';

	$mps = GetAllMP('FY',UserMP());
	$mps = explode(',',str_replace("'",'',$mps));

	foreach($mps as $mp)
		$extra['second_col'] .= '<OPTION value='.$mp.'>'.GetMP($mp);

	$extra['second_col'] .= '</SELECT></TD></TR>';	
	
	$extra['second_col'] .= '</TABLE></CENTER>';

	Widgets('all');
	Widgets('document_template');
	include($staticpath.'modules/misc/Export.php');
}
else
{
	$extra['DATE'] = DBGet(DBQuery("SELECT min(SCHOOL_DATE) AS START_DATE FROM ATTENDANCE_CALENDAR WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."'"));
	$extra['DATE'] = $extra['DATE'][1]['START_DATE'];
	if(!$extra['DATE'] || DBDate('postgres')>$extra['DATE'])
		$extra['DATE'] = DBDate();

	$first_run = true;
	if($_REQUEST['teacher_id'])
		$teachers_extra = " STAFF_ID='".$_REQUEST['teacher_id']."' AND ";
	if($_REQUEST['period_id'])
		$periods_extra = " cp.PERIOD_ID='".$_REQUEST['period_id']."' AND ";

	if($_REQUEST['mp_id'])
	{
		$table = GetMPTable(GetMP($_REQUEST['mp_id'],'TABLE'));
		$mp_list = GetAllMP($table,$_REQUEST['mp_id']);
		$mps_extra = " AND cp.MARKING_PERIOD_ID IN (".$mp_list.") ";
		$extra['mp_list'] = $mp_list;
	}

	$teachers_RET = DBGet(DBQuery("SELECT FIRST_NAME,LAST_NAME,STAFF_ID FROM USERS WHERE $teachers_extra SCHOOLS LIKE '%,".UserSchool().",%' AND SYEAR='".UserSyear()."' AND PROFILE='teacher' ORDER BY LAST_NAME,FIRST_NAME"));
	$course_periods_RET = DBGet(DBQuery("SELECT cp.TITLE,cp.COURSE_PERIOD_ID,cp.PERIOD_ID,cp.MARKING_PERIOD_ID,cp.DAYS,c.TITLE AS COURSE_TITLE,cp.TEACHER_ID FROM COURSE_PERIODS cp,COURSES c WHERE $periods_extra c.COURSE_ID=cp.COURSE_ID AND cp.SYEAR='".UserSyear()."' AND cp.SCHOOL_ID='".UserSchool()."'".$mps_extra),array(),array('TEACHER_ID'));
	
	$handle = PDFStart();
	foreach($teachers_RET as $teacher)
	{
		foreach($course_periods_RET[$teacher['STAFF_ID']] as $course_period)
		{
			unset($_FOCUS['DrawHeader']);
			DrawHeader(Config('TITLE').' '._('Class List'));
			DrawHeader($teacher['FIRST_NAME'].' '.$teacher['LAST_NAME'],$course_period['COURSE_TITLE'].' '.GetPeriod($course_period['PERIOD_ID']).($course_period['MARKING_PERIOD_ID']!='0'?' - '.GetMP($course_period['MARKING_PERIOD_ID']):'').(strlen($course_period['DAYS'])<5?' - '.$course_period['DAYS']:''));
			DrawHeader(GetSchool(UserSchool()),ProperDate(DBDate()));
			
			$_FOCUS['User'] = array(1=>array('STAFF_ID'=>$course_period['TEACHER_ID'],'NAME'=>'name','PROFILE'=>'teacher','SCHOOLS'=>','.UserSchool().',','SYEAR'=>UserSyear()));
			$_SESSION['UserCoursePeriod'] = $course_period['COURSE_PERIOD_ID'];
			
			if(!$first_run)
				$extra = $first_extra;
			else
			{
				$first_extra = $extra;
				$first_run = false;
			}
			
			include($staticpath.'modules/misc/Export.php');

			echo '<!-- NEW PAGE -->';				
		}
	}
	PDFStop($handle);
}
?>